package cn.iocoder.yudao.module.water.service.year;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.water.controller.admin.year.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.year.ReportYearDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.water.convert.year.ReportYearConvert;
import cn.iocoder.yudao.module.water.dal.mysql.year.ReportYearMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.water.enums.ErrorCodeConstants.*;

/**
 * 年报 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class ReportYearServiceImpl implements ReportYearService {

    @Resource
    private ReportYearMapper reportYearMapper;

    @Override
    @DS("report")
    public Long createReportYear(ReportYearCreateReqVO createReqVO) {
        // 插入
        ReportYearDO reportYear = ReportYearConvert.INSTANCE.convert(createReqVO);
        reportYearMapper.insert(reportYear);
        // 返回
        return reportYear.getId();
    }

    @Override
    @DS("report")
    public void updateReportYear(ReportYearUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateReportYearExists(updateReqVO.getId());
        // 更新
        ReportYearDO updateObj = ReportYearConvert.INSTANCE.convert(updateReqVO);
        reportYearMapper.updateById(updateObj);
    }

    @Override
    @DS("report")
    public void deleteReportYear(Long id) {
        // 校验存在
        this.validateReportYearExists(id);
        // 删除
        reportYearMapper.deleteById(id);
    }

    private void validateReportYearExists(Long id) {
        if (reportYearMapper.selectById(id) == null) {
            throw exception(REPORT_YEAR_NOT_EXISTS);
        }
    }

    @Override
    @DS("report")
    public ReportYearDO getReportYear(Long id) {
        return reportYearMapper.selectById(id);
    }

    @Override
    @DS("report")
    public List<ReportYearDO> getReportYearList(Collection<Long> ids) {
        return reportYearMapper.selectBatchIds(ids);
    }

    @Override
    @DS("report")
    public PageResult<ReportYearDO> getReportYearPage(ReportYearPageReqVO pageReqVO) {
        return reportYearMapper.selectPage(pageReqVO);
    }

    @Override
    @DS("report")
    public List<ReportYearDO> getReportYearList(ReportYearExportReqVO exportReqVO) {
        return reportYearMapper.selectList(exportReqVO);
    }

    @Override
    @DS("report")
    public List<ReportYearDO> getReportOneYearList(String date) {
        String start = String.valueOf(DateUtil.beginOfYear(DateUtil.parseDateTime(date)));
        String end = String.valueOf(DateUtil.endOfYear(DateUtil.parseDateTime(date)));
        return reportYearMapper.selectOneYearList(start, end);
    }

}
